package com.game.play;

import java.util.Scanner;

import com.game.cards.BaseCard;
import com.game.cards.Flags;
import com.game.cards.MonsterCard;
import com.game.cards.TrapCard;

public class CommandParser {
	private Game game;
	private ActionQueue aq;
	private Scanner scan;
	public CommandParser(Game game, ActionQueue aq, Scanner scan)
	{
		this.game = game;
		this.aq = aq;
		this.scan = scan;
	}
	public void parse(String s)
	{
		String[] arg = s.split(",");
		int levels = 0;
		int max = arg.length;
		//COMMANDS START
		
		//THE VIEW COMMAND
		if (arg[0].equals("view")) {
			levels++;
			if (arg[1].equals("hand"))
			{
				levels++;
				if (levels == max) 
					game.getCurrentTurn().printHand();
				//TODO Make this support multi card
				else game.getCurrentTurn().getHand().getCards().get(Integer.valueOf(arg[2])).printInfo();
			}
			if (arg[1].equals("graveyard"))
			{
				levels++;
				if (levels == max)
					game.getCurrentTurn().printGraveSimple();
			}
		}
		else if (arg[0].equals("help")){
			
			System.out.println("Type [ play,card ] to play cards");
			System.out.println("Type [ attack ] to attack ");
			
		}
		else if (arg[0].equals("heartofthecards")) {
			//if (game.getCurrentTurn().gs
		}
		else if (arg[0].equals("effect"))
		{
	
			if (game.getCurrentTurn().getField().getEffectMonsterCards().size() > 0){
				System.out.println("Which effect do you want to use? (type -1 to cancel)");
				int count = 0;
				for (MonsterCard c : game.getCurrentTurn().getField().getEffectMonsterCards())
				{
					System.out.println("(" + count + ")" + c.getTitle());
				}
				int input = scan.nextInt();
				if (input != -1) {
					game.getCurrentTurn().getField().getEffectMonsterCards().get(count).effect(game);
				}
				
			}
		}
		//ACTION QUEUE COMMAND
		else if (arg[0].equals("aq"))
		{
			levels++;
			if(levels == arg.length)this.aq.printActions();
			else if (arg[1].equals("recent"))
			{
				levels++;
				if(levels == arg.length)this.aq.printMostRecentAction();
				else {
					this.aq.printMostRecentActions(Integer.valueOf(arg[2]));
				}
			}
			else if (arg[1].equals("actions"))
			{
				levels++;
				if (levels == arg.length) {
					for (TrapCard tc : aq.getActions())
					{
						tc.printInfo();
					}
				}
			}
		}
		//THE PLAY COMMAND
		else if (arg[0].equals("play"))
		{
			levels++;
			if (levels == arg.length) System.out.println("Not enough args.");
			else if (arg[1].equals("monster")) {
				levels++;
				if (levels == arg.length) {
				int i = 0;
				System.out.println("Which Monster?");
				for ( BaseCard c : game.getCurrentTurn().getHand().getMonsterCards()) {
					MonsterCard cd = (MonsterCard) c;
					
					System.out.println("(" + i + ")" + cd.getTitle() + " " + cd.getStars());
					i++;
				}
				MonsterCard mc = (MonsterCard) game.getCurrentTurn().getHand().getMonsterCards().get(scan.nextInt());
				//game.getCurrentTurn().getHand().removeCardById(mc.getId());
				game.getCurrentTurn().getField().normalSummon(mc);
				}
				else {
					MonsterCard mc = (MonsterCard) game.getCurrentTurn().getHand().getMonsterCards().get(Integer.valueOf(arg[2]));
					game.getCurrentTurn().getHand().removeCardById(mc.getId());
					game.getCurrentTurn().getField().normalSummon(mc);
				}
			}
			else if (arg[1].equals("card"))
			{
				levels++;
				if (levels == arg.length)
				{
					System.out.println("Which Card?");
					int i = 0;
					for ( BaseCard cd : game.getCurrentTurn().getHand().getCards()) {
						
						
						System.out.println("(" + i + ")" + cd.getTitle());
						i++;
					}
					BaseCard bc = game.getCurrentTurn().getHand().getCards().get(scan.nextInt());
					//game.getCurrentTurn().getHand().removeCardById(bc.getId());
					game.getCurrentTurn().getField().playCard(bc);
				}
				else {
					BaseCard bc = game.getCurrentTurn().getHand().getCards().get(Integer.valueOf(arg[2]));
					//game.getCurrentTurn().getHand().removeCardById(bc.getId());
					game.getCurrentTurn().getField().playCard(bc);
				}
			}
		}
		
		//THE ATTACK COMMAND
		else if (arg[0].equals("attack"))
		{
			if (game.getTotalTurns() != 0) {
			levels++;
			int i = 0;
			System.out.println("With which Monster? Type -1 to cancel.");
			for ( BaseCard c : game.getCurrentTurn().getField().getMonsterCards()) {
				MonsterCard cd = (MonsterCard) c;
				
				if (cd.getAttacks() > 0)System.out.println("(" + i + ")" + cd.getTitle() + " " + cd.getStars());
				i++;
			}
			int input = scan.nextInt();
			if (input == -1) {}
			else {MonsterCard mc = (MonsterCard) game.getCurrentTurn().getField().getMonsterCards().get(input);
			System.out.println("What are you attacking?");
			if (game.getOffTurn().getField().getMonsterCards().size() == 0 || mc.getFlags().contains(Flags.direct)) {
				System.out.println("(0)Direct Attack");
				if (scan.nextInt() == 0) {
					aq.addEvent(Events.attackplayer +","+mc.getId()+","+game.getOffTurn().getName());
					mc.setAttacks(mc.getAttacks()-1);
					game.attack(mc, game.getOffTurn());
					
				}
			}
			
			else {
				 i= 0;
			for ( BaseCard c : game.getOffTurn().getField().getMonsterCards()) {
				MonsterCard cd = (MonsterCard) c;
				
				System.out.println("(" + i + ")" + cd.getTitle() + " " + cd.getStars());
				i++;
				
				}
			mc.setAttacks(mc.getAttacks()-1);
			game.attack(mc, game.getOffTurn().getField().getMonsterCards().get(scan.nextInt()));
			}
			}
			
		}
		}
	}
	

}
